package com.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.base.po.voip.Shopinfoes;
import com.base.po.voip.ShopinfoesExample;
import com.base.po.voip.ShopinfoesExample.Criteria;
import com.base.po.voip.Users;
import com.base.service.ShopinfoService;
import com.base.service.UsersService;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_badamokuai {
	@Autowired
	private ShopinfoService shopinfoService;
	@Autowired
	private UsersService usersService;

	// 用户首页
	@RequestMapping("/content_badamokuai")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		ShopinfoesExample shopinfoesExample = new ShopinfoesExample();
		shopinfoesExample.setOrderByClause("Grade DESC");
		Criteria a = shopinfoesExample.createCriteria();
		a.andAgentidEqualTo(adminUserId).andTypeEqualTo(1).andDatastatusEqualTo(true);
		System.out.println(adminUserId);
		List<Shopinfoes> shopinfoes = shopinfoService.selectByExample(shopinfoesExample);
		
		Users user = usersService.selectByPrimaryKey(adminUserId);
		Users pUser = usersService.selectByPrimaryKey(user.getParentid());
		
//		if(adminUsergrade == 0) {
		request.setAttribute("shopinfoes", shopinfoes);
//		}
//		if(adminUsergrade == 1) {
//			if(shopinfoes.size()<1) {
//				ShopinfoesExample oemExample = new ShopinfoesExample();
//				shopinfoesExample.setOrderByClause("Grade DESC");
//				Criteria b = oemExample.createCriteria();
//				b.andAgentidEqualTo(user.getParentid()).andTypeEqualTo(1).andDatastatusEqualTo(true);
//				List<Shopinfoes> oemShopinfoes = shopinfoService.selectByExample(oemExample);
//				request.setAttribute("shopinfoes", oemShopinfoes);
//			}else {
//				request.setAttribute("shopinfoes", shopinfoes);
//			}
//		}
//		if(adminUsergrade == 2) {
//			if(shopinfoes.size()<1) {
//				ShopinfoesExample oemExample = new ShopinfoesExample();
//				shopinfoesExample.setOrderByClause("Grade DESC");
//				Criteria b = oemExample.createCriteria();
//				b.andAgentidEqualTo(pUser.getParentid()).andTypeEqualTo(1).andDatastatusEqualTo(true);
//				List<Shopinfoes> oemShopinfoes = shopinfoService.selectByExample(oemExample);
//				
//				if(oemShopinfoes.size()<1) {
//					ShopinfoesExample adminExample = new ShopinfoesExample();
//					adminExample.setOrderByClause("Grade DESC");
//					Criteria c = adminExample.createCriteria();
//					c.andAgentidEqualTo(user.getParentid()).andTypeEqualTo(1).andDatastatusEqualTo(true);
//					List<Shopinfoes> adminShopinfoes = shopinfoService.selectByExample(adminExample);
//					request.setAttribute("shopinfoes", adminShopinfoes);
//				}else {
//					request.setAttribute("shopinfoes", oemShopinfoes);
//				}
//			}else {
//				request.setAttribute("shopinfoes", shopinfoes);
//			}
//		}
		
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/content_badamokuai");
		return modelAndView;
	}
	
	@RequestMapping("/content_badatest")
	public ModelAndView indexTest(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		ShopinfoesExample shopinfoesExample = new ShopinfoesExample();
		shopinfoesExample.setOrderByClause("Grade DESC");
		Criteria a = shopinfoesExample.createCriteria();
		a.andAgentidEqualTo(adminUserId).andTypeEqualTo(1).andDatastatusEqualTo(true);
		System.out.println(adminUserId);
		List<Shopinfoes> shopinfoes = shopinfoService.selectByExample(shopinfoesExample);
		
//		Users user = usersService.selectByPrimaryKey(adminUserId);
		
		request.setAttribute("shopinfoes", shopinfoes);
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/content_badatest");
		return modelAndView;
	}

}
